/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.dispatchers;

import com.sdd.facades.view.CommentViewFacade;
import com.sdd.facades.view.ProductViewFacade;
import com.sdd.plains.CartPlain;
import com.sdd.plains.CommentPlain;
import com.sdd.plains.ProductPlain;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Usuario
 */
@WebServlet(name = "ProductDetail", urlPatterns = {"/ProductDetail"})
public class ProductDetail extends HttpServlet {

    @EJB
    ProductViewFacade productViewFacade;
    @EJB
    CommentViewFacade commentViewFacade;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String idProduct = request.getParameter("idproduct");
        ProductPlain prodPlain = productViewFacade.getProduct(new Long(idProduct));
        List<CommentPlain> comments = commentViewFacade.getAllCommentsOfAProduct(prodPlain);
        request.getSession().setAttribute("product_plain", prodPlain);
        request.getSession().setAttribute("comments", comments);
        //request.getSession().setAttribute("product_categories", prodPlain.getCategories());
        RequestDispatcher r = null;
        r = getServletContext().getRequestDispatcher("/product_detail.jsp");
        r.include(request, response);

    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idProduct = request.getParameter("idproduct");
        ProductPlain prodPlain = productViewFacade.getProduct(new Long(idProduct));
        // Si es la primera cosa que se mete, se crea el carrito.
        if( request.getSession().getAttribute("cart") == null ){
            request.getSession().setAttribute("cart", new CartPlain());
        }
        CartPlain cart = (CartPlain)(request.getSession().getAttribute("cart"));
        cart.addToCart(prodPlain);
        request.getSession().setAttribute("cart", cart);
        PrintWriter out = response.getWriter();
        out.println();
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
